package com.itsu.commons.utils;

import cn.hutool.core.codec.Base64;
import com.baidu.aip.face.AipFace;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Properties;

public class FaceAiUtil {

    private static String appId;
    private static String apiKey;
    private static String apiSecret;

    static {
        Properties properties = new Properties();
        try {
            properties.load(FaceAiUtil.class.getClassLoader().getResourceAsStream("alibabaServer.properties"));
            appId =properties.getProperty("ai_face.app_id");
            apiKey=properties.getProperty("ai_face.api_key");
            apiSecret=properties.getProperty("ai_face.api_secret");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean aiFace(byte[] imageBytes) {
        // 初始化一个AipFace
        AipFace client = new AipFace("25283923", "RWohyC33826G208OPDPQTOCf", "AbNhbkaa0d2HTm4zyEqIeIw2wMbtnaZd");

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

//         可选：设置代理服务器地址, http和socket二选一，或者均不设置
//         设置http代理
//        client.setHttpProxy("proxy_host", proxy_port);
//         设置socket代理
//        client.setSocketProxy("proxy_host", proxy_port);

        // 调用接口

        // 人脸检测
        JSONObject res = client.detect(Base64.encode(imageBytes), "BASE64", null);
        System.out.println(res.toString(2));
        return res.getString("error_msg").equalsIgnoreCase("SUCCESS");
    }
}
